//
//  YHHoemAgentMakeShareMoneyMenuView.m
//  YHApp
//
//  Created by sy on 2023/11/25.
//

#import "HAHoemAgentMakeShareMoneyPopView.h"
#import "HAAgentLevelsMo.h"

@interface HAHoemAgentMakeShareMoneyPopView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation HAHoemAgentMakeShareMoneyPopView

- (instancetype)initWithList:(NSArray *)array
{
    self = [super init];
    if (self) {
        [self.dataArray addObjectsFromArray:array];
        [self layoutViews];
    }
    return self;
}

#pragma mark - 点击事件
- (void)didTapBackgroundView:(UITapGestureRecognizer *)sender {
    [super didTapBackgroundView:sender];
}

-(void)cancelClick {
    [self dismiss];
}

-(void)sureClick {
    HAAgentLevelsMo *selMo;
    for (int i = 0; i < self.dataArray.count; i ++) {
        HAAgentLevelsMo *mo = self.dataArray[i];
        if (mo.isSel) {
            selMo = mo;
            break;
        }
    }
    if (!selMo) {
        [MBProgressHUD showError:@"请选择等级"];
        return;
    }
    if (self.YHChooseTabMenuCellSureBlock) {
        self.YHChooseTabMenuCellSureBlock(selMo);
    }
    [self dismiss];
}

-(void)show {
    [super show];
}
-(void)dismiss {
    [super dismiss];
}


-(void)layoutViews {
    
    self.frame = CGRectMake(0, 0, kUIScreen_w, kUIScreen_h);
    [self addSubview:self.backgroundView];
    
    //顶部视图
    CGFloat alertViewH = self.dataArray.count * 50 + 50 + 20 + app_bottomH;
    if (alertViewH > kUIScreen_h*0.65) {
        alertViewH = kUIScreen_h*0.65;
    }
    if (alertViewH < 280) {
        alertViewH = 280;
    }
    CGFloat alertViewW = kUIScreen_w;
    CGFloat alertViewX = 0;
    CGFloat alertViewY = kUIScreen_h-alertViewH;
    self.alertViewY = alertViewY;
    self.alertView.frame = CGRectMake(alertViewX, alertViewY+20, alertViewW, alertViewH);
    [self addSubview: self.alertView];
    self.alertView.backgroundColor = [UIColor whiteColor];
    self.alertView.layer.cornerRadius = 15;
    self.alertView.layer.masksToBounds = YES;

    UILabel *titLabel = [UILabel ha_NLabelWithFont:18 textColor:@"#111A34" text:@"选择等级" alignment:1];
    [self.alertView addSubview:titLabel];
    [titLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.centerX.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(titLabel.mas_bottom);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-app_bottomH);
    }];
    
    UIButton *cancelButton = [[UIButton alloc]init];
    [self.alertView addSubview:cancelButton];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
    cancelButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [cancelButton addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
    [self.alertView addSubview:cancelButton];
    [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.width.mas_offset(50);
        make.height.mas_offset(50);
    }];
    
    UIButton *sureButton = [[UIButton alloc]init];
    [self.alertView addSubview:sureButton];
    [sureButton setTitle:@"确定" forState:UIControlStateNormal];
    [sureButton setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
    sureButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [sureButton addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
    [self.alertView addSubview:sureButton];
    [sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.width.mas_offset(50);
        make.height.mas_offset(50);
    }];
}

#pragma mark - Table view data source
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *myCcell = [tableView dequeueReusableCellWithIdentifier:@"HAChooseTabPopViewCellID"];
    if (!myCcell) {
        myCcell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HAChooseTabPopViewCellID"];
    }
    myCcell.selectionStyle = UITableViewCellSelectionStyleNone;
    BOOL isline = YES;
    if (indexPath.row == self.dataArray.count-1) {
        isline = NO;
    }
    [self pp_setCellWith:myCcell.contentView model:self.dataArray[indexPath.row] isLine:isline];
    return myCcell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    for (HAAgentLevelsMo *mo in self.dataArray) {
        mo.isSel = NO;
    }
    HAAgentLevelsMo *mo = self.dataArray[indexPath.row];
    mo.isSel = YES;
    [self.tableView reloadData];
}

-(void)pp_setCellWith:(UIView *)contentView model:(HAAgentLevelsMo *)model isLine:(BOOL)isLine{
    for (UIView *view in contentView.subviews) {
        [view removeFromSuperview];
    }
    
    UILabel *titLabel = [UILabel ha_NLabelWithFont:16 textColor:@"#333333" text:model.name alignment:1];
    [contentView addSubview:titLabel];
    [titLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.centerY.mas_equalTo(0);
    }];
    
    UIImageView *imgView = [UIImageView ha_createImageView];
    [contentView addSubview:imgView];
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(0);
        make.width.mas_equalTo(16);
        make.height.mas_equalTo(16);
    }];

    
    if (model.isSel == NO) {
        imgView.image = [UIImage imageNamed:@""];
        titLabel.textColor = [UIColor colorWithHexString:@"#999999"];
    }else {
        imgView.image = [UIImage imageNamed:@"ha_1_sel"];
        titLabel.textColor = [UIColor colorWithHexString:@"#333333"];
    }
    
    if (isLine) {
        UIView *lineView = [UIView ha_createView];
        lineView.backgroundColor = [UIColor khaBGColor];
        [contentView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.mas_equalTo(0);
            make.height.mas_offset(1);
        }];
    }
}
- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        // 去掉滚动条
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.alertView addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}




@end
